// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成

package com.fowo.api.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.*;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.annotaion.DataObjectType;
import com.fowo.api.entity.AmazonSettlementReport;
import com.fowo.api.model.amazon.settlement.report.AmazonSettlementReportItemVo;
import com.fowo.api.model.amazon.settlement.report.AmazonSettlementReportSearchParamPo;
import com.fowo.api.model.bd.rate.BdRateSelectBdRateDetailParam;
import com.fowo.api.model.bd.rate.BdRateSelectBdRateDetailResult;

import java.math.BigDecimal;
import java.util.List;
import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 亚马逊结算中心-交易明细 服务实现类
 *
 * @author 蜂窝蜜造平台 配置人：鹿丙涛，任凯
 */
@Service
@Slf4j
@DataObjectType(AmazonSettlementReport.class)
@DS("ds24")
public class AmazonSettlementReportServiceImpl extends AmazonSettlementReportBaseServiceImpl {
    @Resource
    private BdRateServiceImpl bdRateService;


    @Override
    public Page<AmazonSettlementReportItemVo> pageSearch(AmazonSettlementReportSearchParamPo search) {
        final List<String> currencyTypeInList = search.getCurrencyCodeInList();
        search.setCurrencyCodeInList(null);
        return this.translatRate(CollUtil.getFirst(currencyTypeInList), super.pageSearch(search));
    }

    @Override
    public Page<AmazonSettlementReportItemVo> transactionDetailsPage(
            AmazonSettlementReportSearchParamPo search
    ) {
        final List<String> currencyTypeInList = search.getCurrencyCodeInList();
        search.setCurrencyCodeInList(null);
        return this.translatRate(CollUtil.getFirst(currencyTypeInList), super.transactionDetailsPage(search));
    }

    /**
     * 根据币种转换汇率
     *
     * @param currency
     * @param page
     * @return
     */
    private Page<AmazonSettlementReportItemVo> translatRate(String currency, Page<AmazonSettlementReportItemVo> page) {
        if (CollUtil.isEmpty(page.getRecords())) {
            return page;
        }
        for (AmazonSettlementReportItemVo item : page.getRecords()) {
            BigDecimal ratio = BigDecimal.ONE;
            if (StrUtil.isNotBlank(currency)) {
                BdRateSelectBdRateDetailParam param = new BdRateSelectBdRateDetailParam();
                param.setFromcurrency(item.getCurrencyCode());
                param.setTocurrency(currency);
                param.setDate(DateUtil.format(item.getPostedDateLocale(), DatePattern.NORM_MONTH_PATTERN));
                BdRateSelectBdRateDetailResult rate = bdRateService.selectBdRateDetail(param);
                if (ObjUtil.isEmpty(rate)) {
                    continue;
                }
                ratio = Convert.toBigDecimal(rate.getRatio()); //转换汇率
                item.setCurrencyCode(rate.getTocurrency()); //转换币种
            }
            //汇率计算和赋值
            item.setCurrencyAmount(NumberUtil.mul(item.getCurrencyAmount(), ratio));
        }
        return page;
    }

}